//
//  ModifyPassworkViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/8.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ModifyPassworkViewController.h"

@interface ModifyPassworkViewController ()<UIAlertViewDelegate,UITextFieldDelegate>
{
    NSMutableString * passWordStr;
}
@end

@implementation ModifyPassworkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
  //  [self addBackItem];
    self.navigationItem.title = @"修改密码";
   // [self addNavgationBar];
    // Do any additional setup after loading the view.
}
-(void)main
{
    passWordStr = [[NSMutableString alloc]init];

    
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray * nameArr = @[@"请输入原始密码",@"请输入新密码",@"请确认新密码"];
    
    for (int i = 0; i<3; i++) {
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(SIZETO6W(30), 64 + SIZETO6H(48)+ (SIZETO6H(15)+40)*i, SCREEN_WIDTH-SIZETO6W(30)*2, 40)];
        textField.placeholder = nameArr[i];
        textField.tag = i + 10;
        textField.secureTextEntry = YES;
        textField.delegate = self;
        textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        [self.view addSubview:textField];
        
        UIView * lineV = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(30), textField.bottom + SIZETO6H(5), SCREEN_WIDTH-SIZETO6W(30)*2, 1)];
        lineV.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:lineV];
        textField.keyboardType = UIKeyboardTypeASCIICapable;

        if (i == 1) {
            [textField addTarget:self action:@selector(NumChange:) forControlEvents:UIControlEventEditingChanged];
        }
        
        if (i == 2) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(SIZETO6W(15), lineV.bottom+SIZETO6H(41), SCREEN_WIDTH-SIZETO6W(15)*2, 45);
            [btn setBackgroundColor:UIColorFromRGB(0x71cc46)];
            [btn setTitle:@"提交" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 20;
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.view addSubview:btn];
        }
        
    }
    
    
}
-(void)NumChange:(UITextField *)textField
{
    if (textField.text.length > passWordStr.length) {
        if (textField.text.length <= 16) {
            
            NSString * s = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
            if ([s isEqualToString:@"&"]) {
                
            }
            else
            {
                [passWordStr appendString:s];
            }
            
        }
    }
    else if([textField.text isEqualToString:@""])
    {
        passWordStr = [[NSMutableString alloc]init];
    }
    else
    {
        NSString * str = [passWordStr substringToIndex:passWordStr.length-1];
        passWordStr = [[NSMutableString alloc]initWithString:str];
    }
    textField.text = passWordStr;
    
}

-(void)submitAction:(UIButton *)btn
{
    UITextField * passwordO = (UITextField *)[self.view viewWithTag:10];
    
    UITextField * passwordT = (UITextField *)[self.view viewWithTag:11];
    
    UITextField * passwordTh = (UITextField *)[self.view viewWithTag:12];
    
    if ([passwordO.text isEqualToString:@""] || [passwordT.text isEqualToString:@""] || [passwordTh.text isEqualToString:@""])
    {
        [Singleton alerSendMessage:@"不能为空"];
    }
    else
    {
        if ([passwordT.text  isEqualToString:passwordTh.text] && passwordT.text.length >=6) {
            [self modifyPasswordWitholdPassword:passwordO.text newPassword:passwordT.text];
        }
        else if(passwordT.text.length < 6)
        {
            [Singleton alerSendMessage:@"6-16位密码"];
        }
        else
        {
            [Singleton alerSendMessage:@"两次新密码保持一致"];
        }
    }
    
}
-(void)modifyPasswordWitholdPassword:(NSString *)oldPassword newPassword:(NSString *)newPassword
{
    if ([self isLogin]) {
        [U76HONet modifyPasswordWtihAction:@"" username:[Singleton share].mobile userpwd:oldPassword userpwd2:newPassword andDataBlock:^(NSDictionary *dic) {
            NSLog(@"修改密码成功了");
            [Singleton alerSendMessage:@"修改密码成功"];
        }];
    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        //        NSLog(@"开始登录了");
        
        if (textField.tag == 10) {
            UITextField * textField1 = (UITextField *)[self.view viewWithTag:11];
            [textField1 becomeFirstResponder];
        }
        else if (textField.tag == 11)
        {
            UITextField * textField = (UITextField *)[self.view viewWithTag:12];
            [textField becomeFirstResponder];
        }
        else if (textField.tag == 12)
        {
            UITextField * passwordO = (UITextField *)[self.view viewWithTag:10];
            
            UITextField * passwordT = (UITextField *)[self.view viewWithTag:11];
            
            UITextField * passwordTh = (UITextField *)[self.view viewWithTag:12];
            
            if ([passwordO.text isEqualToString:@""] || [passwordT.text isEqualToString:@""] || [passwordTh.text isEqualToString:@""])
            {
                [Singleton alerSendMessage:@"不能为空"];
            }
            else
            {
                if ([passwordT.text  isEqualToString:passwordTh.text] && passwordT.text.length >=6) {
                    [self modifyPasswordWitholdPassword:passwordO.text newPassword:passwordT.text];
                }
                else if(passwordT.text.length < 6)
                {
                    [Singleton alerSendMessage:@"6-16位密码"];
                }
                else
                {
                    [Singleton alerSendMessage:@"两次新密码保持一致"];
                }
            }
        }
        
        return NO;
    }
    return YES;
    
}

#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
